package com.bank.ui.admin;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;

public class GeneralQueryDialog extends JDialog {
    private final JTextField queryField;
    private final JComboBox<String> queryTypeComboBox;
    private final JTable resultTable;
    private final QueryHandler queryHandler;

    public GeneralQueryDialog(QueryHandler queryHandler) {
        this.queryHandler = queryHandler;
        setTitle("查询");
        setSize(600, 400);
        setModal(true);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout(10, 10));
        setResizable(false);

        // 查询面板
        JPanel queryPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
        queryPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        queryTypeComboBox = new JComboBox<>(queryHandler.getQueryTypes());
        queryTypeComboBox.setPreferredSize(new Dimension(180, 30));
        queryPanel.add(new JLabel("查询类型:"));
        queryPanel.add(queryTypeComboBox);

        queryField = new JTextField(20);
        queryField.setPreferredSize(new Dimension(200, 30));
        queryPanel.add(new JLabel("查询条件:"));
        queryPanel.add(queryField);

        JButton queryButton = new JButton("查询");
        queryButton.setPreferredSize(new Dimension(80, 30));
        queryButton.addActionListener(this::performQuery);
        queryPanel.add(queryButton);

        add(queryPanel, BorderLayout.NORTH);

        // 结果表格
        resultTable = new JTable();
        resultTable.setFont(new Font("宋体", Font.PLAIN, 14));
        resultTable.getTableHeader().setFont(new Font("宋体", Font.BOLD, 14));
        resultTable.setRowHeight(25);

        JScrollPane scrollPane = new JScrollPane(resultTable);
        scrollPane.setBorder(BorderFactory.createTitledBorder("查询结果"));
        add(scrollPane, BorderLayout.CENTER);

        // 底部按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
        JButton closeButton = new JButton("关闭");
        closeButton.addActionListener(e -> dispose());
        buttonPanel.add(closeButton);

        add(buttonPanel, BorderLayout.SOUTH);
    }

    private void performQuery(ActionEvent e) {
        String queryText = queryField.getText().trim();
        String queryType = (String) queryTypeComboBox.getSelectedItem();

        if (queryText.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入查询条件！", "输入错误", JOptionPane.WARNING_MESSAGE);
            return;
        }

        try {
            List<Object> results = queryHandler.performQuery(queryType, queryText);
            // 更新表格模型
            updateResultTable(results, queryHandler.getColumnNames());
        } catch (SQLException | NumberFormatException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(
                    this,
                    "查询失败: " + ex.getMessage(),
                    "错误",
                    JOptionPane.ERROR_MESSAGE
            );
        }
    }

    private void updateResultTable(List<Object> results, String[] columnNames) {
        DefaultTableModel model = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false; // 表格不可编辑
            }
        };

        // 添加数据
        if (results != null && !results.isEmpty()) {
            for (Object result : results) {
                model.addRow(queryHandler.convertToTableRow(result));
            }
        } else {
            model.addRow(new Object[]{"", "没有找到匹配的记录", "", "", ""});
        }

        resultTable.setModel(model);
    }

    public interface QueryHandler {
        String[] getQueryTypes();
        List<Object> performQuery(String queryType, String queryText) throws SQLException;
        String[] getColumnNames();
        Object[] convertToTableRow(Object result);
    }
}